package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private ApartmentInfoService apartmentInfoService;

    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper;
    @Autowired
    private RoomFacilityMapper roomFacilityMapper;
    @Autowired
    private RoomLabelMapper roomLabelMapper;
    @Autowired
    private RoomPaymentTypeMapper roomPaymentTypeMapper;
    @Autowired
    private  ApartmentFeeValueMapper apartmentFeeValueMapper;
    @Autowired
    private RoomLeaseTermMapper roomLeaseTermMapper;
    @Autowired
    private ApartmentLabelMapper apartmentLabelMapper;
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;


    @Override
    public void queryRoomList(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        //通过地区，价格，付款方式查询---按照价格排序

        List<RoomItemVo> roomItemVos = roomInfoMapper.queryRoomInfoList(page, queryVo).getRecords();
        for (RoomItemVo roomItemVo : roomItemVos) {
            roomItemVo.setGraphVoList(graphInfoMapper.graphListByItem(roomItemVo.getId(),ItemType.ROOM));
            roomItemVo.setLabelInfoList(roomLabelMapper.getLableInfoListByRoomId(roomItemVo.getId()));
        }
    }


    @Override
    public RoomDetailVo getDetailById(Long id) {

        //1，房间
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        if (ObjectUtils.isEmpty(roomInfo)) {
            return null;
        }
        //2.公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(roomInfo.getApartmentId());
        if(ObjectUtils.isEmpty(apartmentInfo)){
            return null;
        }
        ApartmentItemVo apartmentItemVo=new ApartmentItemVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentItemVo);
        //2.2公寓标签列表
        apartmentItemVo.setLabelInfoList(apartmentLabelMapper.queryListByApartmentId(roomInfo.getApartmentId()));
        //2.3公寓图片列表
        apartmentItemVo.setGraphVoList(graphInfoMapper.graphListByItem(id,ItemType.ROOM));
        //2.4公寓最小租金
        apartmentItemVo.setMinRent(roomInfoMapper.getminRentByApartId(roomInfo.getApartmentId()));


        //3.房间图片列表
        List<GraphVo> graphVoList = graphInfoMapper.graphListByItem(id,ItemType.ROOM);

        //4.房间属性
        List<AttrValueVo> attrvalueVoList = roomAttrValueMapper.getAttrInfoByRoomId(id);

        //5.配套信息
        List<FacilityInfo> facilityInfoList = roomFacilityMapper.getInfoByRoomId(id);

        //6.标签
        List<LabelInfo> labelInfoList = roomLabelMapper.getLableInfoListByRoomId(id);

        //7.支付方式
        List<PaymentType> paymentTypeList = roomPaymentTypeMapper.payListByRoomId(id);

        //8.杂费列表
        List<FeeValueVo> feeValueVoList = apartmentFeeValueMapper.queryListByApartmentId(roomInfo.getApartmentId());

        //9.租期列表
        List<LeaseTerm> leaseTermList = roomLeaseTermMapper.getInfoByRoomId(id);

        //10.房间是否已入住
        LambdaQueryWrapper<LeaseAgreement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LeaseAgreement::getRoomId, roomInfo.getId());
        queryWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING,LeaseStatus.RENEWING);
        Long singedCount = leaseAgreementMapper.selectCount(queryWrapper);



        RoomDetailVo roomDetailVo=new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setIsDelete(roomInfo.getIsDeleted() == 1);
        roomDetailVo.setIsCheckIn(singedCount > 0);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrvalueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        roomDetailVo.setLeaseTermList(leaseTermList);


        return roomDetailVo;
    }

    @Override
    public void pageItemByApartmentId(IPage<RoomItemVo> page, Long id) {
        List<RoomItemVo> roomItemVos = roomInfoMapper.pageItemByApartmentId(page, id).getRecords();
        for (RoomItemVo roomItemVo : roomItemVos) {
            roomItemVo.setGraphVoList(graphInfoMapper.graphListByItem(roomItemVo.getId(),ItemType.ROOM));
            roomItemVo.setLabelInfoList(roomLabelMapper.getLableInfoListByRoomId(roomItemVo.getId()));
        }

    }

}




